package com.gjy.spring.s11;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Proxy;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-12-20 15:14:50
 */
public class JdkProxyDemo {
    private static final Logger log = LoggerFactory.getLogger(JdkProxyDemo.class);

    interface Foo {
        void foo();
    }

    static final class Target implements Foo {

        @Override
        public void foo() {
            System.out.println("target foo");
        }
    }

    // jdk代理只针对接口
    public static void main(String[] args) throws IOException {
        Target target = new Target();

        ClassLoader classLoader = JdkProxyDemo.class.getClassLoader();
        //proxy:代理对象自己, method:正在执行的方法, arg:方法参数
        Foo foo = (Foo) Proxy.newProxyInstance(classLoader, new Class[]{Foo.class}, (proxy, method, arg) -> {
            log.info("proxy:{}, method:{}, arg:{}", proxy, method.getName(), arg);
            return method.invoke(target, arg);
        });
        System.out.println(foo.getClass());
        foo.foo();

        System.in.read();
    }
}
